package com.bonuo.crm.service.impl;

import com.bonuo.crm.domain.Project;
import com.bonuo.crm.domain.vo.CountAmount;
import com.bonuo.crm.mapper.ActivityMapper;
import com.bonuo.crm.mapper.ProcessMapper;
import com.bonuo.crm.mapper.ProjectMapper;
import com.bonuo.crm.service.ProjectService;
import com.ruoyi.common.annotation.DataScope;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.util.List;

@Service
public class ProjectServiceImpl implements ProjectService {
    @Autowired
    private ProjectMapper projectMapper;
    @Override
    @DataScope(provinceAlias = {"cli","age"})
    public List<Project> selectProjectList(Project project) {
        //查询项目进度有已开票的信息
        if(project.getFlag() != null){
            List<Project> projectList = projectMapper.selectProcessListByInvoice(project);
            return projectList;
        }
        return projectMapper.selectProjectList(project);
    }

    @Override
    public Integer insertProject(Project project) {
        projectMapper.insertProject(project);
        return project.getId();
    }

    @Override
    public Integer deleteProjectById(Integer id) {
        return projectMapper.deleteProjectById(id);
    }

    @Override
    public Integer updateProject(Project project) {
        //如果进度是已签合同，那么把签合同时间设置为当前时间
        if(project.getSchedule() == 14){
            if(project.getSignDate() == null) {
                project.setSignDate(project.getNowDate());
            }
        }
        return projectMapper.updateProject(project);
    }

    @Override
    public Integer selectProjectByProjectName(String name) {
        return projectMapper.selectProjectByProjectName(name);
    }

    @Override
    public Integer selectProjectByContactIdAndMonth(Integer contactId) {
        return projectMapper.selectProjectByContactIdAndMonth(contactId);
    }

    @Override
    public CountAmount selectAmount(List<Integer> ids) {
        // 新增校验：若 id 列表为空，直接返回 0（或根据业务返回 null）
        if (CollectionUtils.isEmpty(ids)) {
            return new CountAmount(BigDecimal.ZERO, BigDecimal.ZERO);
        }
        return projectMapper.selectAmount(ids);
    }

    @Override
    public String selectProjectByProjectNameOrNo(Project project) {
        return projectMapper.selectProjectByProjectNameOrNo(project);
    }

    @Override
    public BigDecimal selectAmountByNotBid(List<Integer> ids) {
        // 新增校验：若 id 列表为空，直接返回 0（或根据业务返回 null）
        if (CollectionUtils.isEmpty(ids)) {
            return BigDecimal.ZERO;
        }

        return projectMapper.selectAmountByNotBid(ids);
    }

    @Override
    public BigDecimal selectAmountByStop(List<Integer> ids) {
        // 新增校验：若 id 列表为空，直接返回 0（或根据业务返回 null）
        if (CollectionUtils.isEmpty(ids)) {
            return BigDecimal.ZERO;
        }
        return projectMapper.selectAmountByStop(ids);
    }

    @Override
    public List<Integer> selectBNProjectIds() {
        return projectMapper.selectBNProjectIds();
    }

    @Override
    @DataScope(provinceAlias = {"cli","age"})
    public List<Integer> selectProjectListIds(Project project,List<Integer> bnProjectIds) {
        return projectMapper.selectProjectListIds(project,bnProjectIds);
    }

}
